package com.neuedu.base;

import com.alibaba.fastjson2.JSONObject;

/**
 * @author 许中
 * @date 2024-09-06 09:22:08
 * 链式编程 每个set方法都返回this当前对象
 * 这样就能一直“点下去”。方便设置值。
 *
 * 希望服务器所有的接口返回的数据格式统一
 * {
 *     "code":200,
 *     "msg":"请求成功",
 *     "data":{
 *         "pageCount":10,
 *         "pageNo":5,
 *         "pageData":[
 *              {
 *                  "areaName":"瑾桦王府",
 *                  "icon":"xxxxxx.jpg",
 *                  ....
 *              }
 *              ,
 *              {
 *                  "areaName":"瑾桦王府",
 *                  "icon":"xxxxxx.jpg",
 *                  ....
 *              }
 *         ]
 *     }
 * }
 *
 *
 *
 *  * {
 *  *     "code":200,
 *  *     "msg":"请求成功",
 *  *     "data":{
 *             "user":{
 *                 "username":"zhansgan",
 *                 "userId":1
 *                  .....
 *             }
 *  *     }
 *  * }
 */
public class R {
    public static final String OK_MSG="请求成功";
    public static final String ERROR_MSG="请求失败";
    public static final int OK_CODE=200;
    public static final int ERROR_CODE=500;

    private Integer code;
    private String msg;
    private Object data;

//    public static void main(String[] args) {
//        R r = R.ok().setData(new Integer(1));
//        System.out.println(r);
//    }
    public static R ok(){
        R r=new R();
        r.code=OK_CODE;
        r.msg=OK_MSG;
        return r;
    }
    public static R ok(int code){
        R r=new R();
        r.code=code;
        r.msg=OK_MSG;
        return r;
    }
    public static R ok(String msg){
        R r=new R();
        r.code=OK_CODE;
        r.msg=msg;
        return r;
    }
    public static R ok(int code,String msg){
        R r=new R();
        r.code=code;
        r.msg=msg;
        return r;
    }

    public static R error(){
        R r=new R();
        r.code=ERROR_CODE;
        r.msg=ERROR_MSG;
        return r;
    }
    public static R error(int code){
        R r=new R();
        r.code=code;
        r.msg=ERROR_MSG;
        return r;
    }
    public static R error(String msg){
        R r=new R();
        r.code=ERROR_CODE;
        r.msg=msg;
        return r;
    }
    public static R error(int code,String msg){
        R r=new R();
        r.code=code;
        r.msg=msg;
        return r;
    }
    public R setCode(Integer code) {
        this.code = code;
        return this;
    }

    public R setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public R setData(Object data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
